﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using PccCommonForC;

public partial class Shopping_Stall_ProductDetail : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        ViewState["IsSaleOf"] = "";
        if (!IsPostBack)
        {
            if (Request.QueryString["IndexTag"] != "" && Request.QueryString["IndexTag"] != null)
                hIndexTag.Value = Request.QueryString["IndexTag"];
            BindData();

        }
    }

    private void BindQuantityData()
    {
        string sSession="dtBasket";
        if(lblKindNo.Text=="F")
            sSession = "dtBasketFilm";

        DataTable dtBasket = new DataTable();

        if (Session[sSession] != null)
        {
            dtBasket = (DataTable)Session[sSession];
        }
        else
        {
            dtBasket.Columns.Add("PDID");
            dtBasket.Columns.Add("PDNAME");
            dtBasket.Columns.Add("PRICE");
            dtBasket.Columns.Add("QUANTITY");
            dtBasket.Columns.Add("RATE");
            dtBasket.Columns.Add("KINDNO");
            dtBasket.Columns.Add("CAPACITY");
        }
        if (txtQuantity.Text.Length <= 0)
        {
            ClientScript.RegisterClientScriptBlock(this.GetType(), "", "<script>alert('Nhập vào số lượng')</script>");
            return;
        }
        clsCommon clsCom = new clsCommon();
        int index = clsCom.SearchDataTable(dtBasket, "PDID", Request.QueryString["PDID"]);
        if (index != -1) //Neu la loai san pham da ton tai thi cho phep so luong
        {
            if (lblKindNo.Text.Trim() == "P")
            {
                int QuantityList = int.Parse(dtBasket.Rows[index]["QUANTITY"].ToString());
                dtBasket.Rows[index]["QUANTITY"] = QuantityList + int.Parse(txtQuantity.Text);
            }
        }
        else
        {
            DataRow row = dtBasket.NewRow();
            string Price = lblDongia.Text;
            string Rate = lblRate.Text;
            if (ViewState["IsSaleOf"].ToString() == "Y")
            {
                Price = lblDiscountPrice.Text;
                Rate = lblDiscountRate.Text;
            }
            row["PDID"] = Request.QueryString["PDID"];
            row["PDNAME"] = lblTenSP.Text;
            row["PRICE"] = Price;
            row["QUANTITY"] = txtQuantity.Text;
            row["RATE"] = Rate;
            row["KINDNO"] = lblKindNo.Text;
            row["CAPACITY"] = lblCapacity.Text;
            dtBasket.Rows.Add(row);

        }
        Session[sSession] = dtBasket;
        Response.Redirect(ResolveUrl("~/") + "Cart/"+lblKindNo.Text+".aspx");

    }


    private void BindData()
    {
        clsWebShop cls = new clsWebShop();
        PccMsg msg = new PccMsg();
        msg.CreateFirstNode("PdID", cls.killSQLInjection(Request.QueryString["PDID"]));
        DataTable dt = cls.Get_ProductByProductID(msg.GetXmlStr.ToString()).Tables[0];
        if (dt.Rows.Count > 0)
        {
            string Url = ResolveUrl("~/");

            lblKind.Text = "<a href='" + Url + "Stall/" + dt.Rows[0]["TopicID"].ToString() + "/0/" + dt.Rows[0]["TopicName"].ToString() + "/0.aspx' class='cssPathLink'>" + dt.Rows[0]["TopicName"].ToString() + "</a><span class='cssPathLink' style='color:green'> > </span><a href='" + Url + "Stall/0/" + dt.Rows[0]["CatID"].ToString() + "/" + dt.Rows[0]["TopicName"].ToString() + "/" + dt.Rows[0]["CatName"].ToString() + ".aspx' class='cssPathLink'>" + dt.Rows[0]["CatName"].ToString() + "</a>";
            lblTenSP.Text = dt.Rows[0]["PDName"].ToString();
            lblWarranty.Text = dt.Rows[0]["Warranty"].ToString();
            lblDongia.Text = FormatMoney(dt.Rows[0]["Price"].ToString());

            lblRate.Text = dt.Rows[0]["Rate"].ToString();
            lblKindNo.Text = dt.Rows[0]["KindNo"].ToString();
            //saleof price
            lblDiscountPrice.Text = dt.Rows[0]["DiscountPrice"].ToString();
            lblDiscountRate.Text = dt.Rows[0]["DiscountRate"].ToString();
            if (lblKindNo.Text != "P")
            {
                /*txtQuantity.ReadOnly = true;
                txtQuantity.CssClass = "txt_ReadOnly";
                txtQuantity.BackColor = System.Drawing.Color.FromName("#eaeaea");
                trBaoHanh.Visible = false;
                trGiaThamKhao.Visible = false;*/
                pnlKindNo.Visible = false;
                pnlFilm.Visible = true;
                lblCapacity.Text = dt.Rows[0]["Capacity"].ToString();
            }
            //SaleOf
            ViewState["IsSaleOf"] = "N";
            if (dt.Rows[0]["IsSaleOf"].ToString() == "Y")
            {
                ViewState["IsSaleOf"] = "Y";
                pnlSaleOf.Visible = true;
                lblDiscountPrice.Text = FormatMoney(lblDiscountPrice.Text);
                pnlPrice.Attributes.Add("class", "td_linethrought");
            }
            else
            {
                pnlSaleOf.Visible = false;
            }
            if (dt.Rows[0]["Status"].ToString() == "C")
            {
                pnlPrice.Attributes.Add("class", "td_linethrought");
            }

            string ImgUrl = dt.Rows[0]["imgURL"].ToString();


            string img = ResolveUrl("~/") + ImgUrl;
            if (ImgUrl == "" || ImgUrl == null)
                img = ResolveUrl("~/") + "Images/Body/Noimages.gif";


            string HTML = "";

            HTML += "<a href='" + img + "'  class='customGal' caption='" + lblTenSP.Text + "'>";
            HTML += "<img  src='" + img + "'  alt='' title='Click to enlarge' border='0'  class='img_ProductDetail' align='left' style='padding-right:5px'>";
            HTML += "</a>";

            ltlImages.Text = HTML;

        }


        string PDID = cls.killSQLInjection((Request.QueryString["PDID"]));

        DataSet ds = new DataSet();
        string strSQL = " ";
      // strSQL +="      select count(*) CountItem from product_img where  pdid='" + PDID + "'   ";
        strSQL += "       SELECT count(*) CountItem   FROM [Product_Comment] where pdid='" + PDID + "'   ";
        strSQL += "        select count(*) CountItem from product where Status<>'D' and PdID<>'" + PDID + "' and catid=(select catid from product where pdid='" + PDID + "')     ";
        ds = cls.GetData(strSQL);

        //lblCTagImg.Text = ds.Tables[0].Rows[0]["CountItem"].ToString();
        lblCTagComment.Text = ds.Tables[0].Rows[0]["CountItem"].ToString();
        string viewcount = System.Configuration.ConfigurationManager.AppSettings["SimilarPD"];
        if (int.Parse(ds.Tables[1].Rows[0]["CountItem"].ToString()) > int.Parse(viewcount))
            lblCTagPD.Text = viewcount;
        else
            lblCTagPD.Text = ds.Tables[1].Rows[0]["CountItem"].ToString();





    }


    protected void ImgGioHang_Click(object sender, ImageClickEventArgs e)
    {
        BindQuantityData();

    }

    private string FormatMoney(string sNum)
    {
        string sReturn = "0";
        try
        {
            sReturn = string.Format("{0:###,###,##0}", decimal.Parse(sNum));
        }
        catch (Exception) { }
        return sReturn;
    }


    private string FormatMoney(decimal dNum)
    {
        string sReturn = "0";
        try
        {
            sReturn = string.Format("{0:###,###,##0}", dNum);
        }
        catch (Exception) { }
        return sReturn;
    }

}
